<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>server</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add" auth="server:add">添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="editwh" auth="server:editwh">批量维护</button>
                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="stopWh" auth="server:stopWh">批量取消维护</button>
                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="addkq" auth="server:addkq">批量开启</button>

            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" auth="server:edit">编辑</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="maintain" auth="server:maintain">维护</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="stopMaintain" auth="server:stopMaintain">取消维护</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete" auth="server:delete">删除</a>
        </script>
    </div>
</div>

<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            services: JSON.parse(localStorage.getItem('services')),
        },
        mounted:function () {
            layui.use(['form', 'table','laydate'], function () {
                var isLodedTable = false;
                var $ = layui.jquery,
                    form = layui.form,
                    table = layui.table
                    ,laydate=layui.laydate;

                laydate.render({
                    elem: '#createTime0',
                    value: getYmd(0)
                });
                laydate.render({
                    elem: '#createTime1',
                    value: getYmd(1)
                });

                var loadTable = function(){
                    table.render({
                        where: form.val('table-search'),
                        elem: '#currentTableId',
                        width:2000,
                        url: '/admin/server/paginate',
                        defaultToolbar: ['filter', 'print' ],
                        toolbar: '#toolbarDemo',
                        cols: [[
                            {type: "checkbox", width: 50},
                            {field: 'server_id', title:"区服ID", width: 100},
                            {title: '服名',width: 150, field:"name"},
                            {title: '服务器信息',width: 150, field:"server_info"},
                            {title: '分组名', width: 150, field:"group_name",templet:function (row) {
                                return row.group?row.group.name:'';
                                }},
                            {title:"推送地址",width: 150,field: 'push_url'  },
                            {title:"客户端登录IP",width: 150,field: 'login_ip'  },
                            {title:"客户端登录端口",width: 100,field: 'login_port' },
                            {field: '是否合区', width: 80, title: '显示状态',templet:function (row) {
                                    return row.is_merge===1 ? '合区': '没合区';
                                }},
                            {field: 'show', width: 80, title: '是否合区',templet:function (row) {
                                    return row.show===1 ? '显示': '隐藏';
                                }},
                            {field: 'busy_status', width: 80 , title: '繁忙状态',templet:function (row) {
                                    switch(row.busy_status){
                                        case 1: return '良好';
                                        case 2: return '繁忙';
                                        case 3: return '爆满';
                                    }
                                }},
                            {field: 'status' , width: 80, title: '维护状态',templet:function (row) {
                                    switch(row.status){
                                        case 1: return '开启';
                                        case 2: return '维护';
                                        case 3: return '预开服';
                                        case 4: return '关闭';
                                    }
                                }},
                            {field: 'pay_status' , width: 80, title: '充值状态',templet:function (row) {
                                    switch(row.pay_status){
                                        case 0: return '关闭';
                                        case 1: return '开启';
                                    }
                                }},
                            {title:"开服时间",field: 'enable_at' ,},
                            {title:"维护时间",field: 'maintain_at', width: 200,},
                            {title: '操作', minWidth: 300, toolbar: '#currentTableBar', align: "center"}
                        ]],
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 15,
                        page: true,
                        skin: 'line'
                    });
                    isLodedTable = true;
                }

                loadTable();
                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    if (!isLodedTable){
                        loadTable();
                    } else {
                        //执行搜索重载
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                            , where: form.val('table-search')
                        }, 'data');
                    }
                    return false;
                });



                table.on('toolbar(currentTableFilter)',function (obj) {
                    var selectedData = table.checkStatus(obj.config.id).data;
                    var serverIds = selectedData.map(function(data) {
                        return data.server_id;
                    }).join(',');
                    var names = selectedData.map(function(data) {
                        return data.name;
                    }).join('|');
                    var namesEncoded = encodeURIComponent(names);
                    console.log('查看标题',obj.event)
                    if(obj.event === 'add'){ //添加
                        console.log('进来没：');
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '700px'],
                            title: '添加区服',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "detail.html"
                        });
                    }

                    if(obj.event === 'editwh'){ //维护
                        layer.open({
                            type: 2, // page 层类型取消
                            area: ['780px', '700px'],
                            title: '批量维护',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "maintain.html?serverIds=" + serverIds + "&name=" + namesEncoded
                        });
                    }
                    if(obj.event === 'stopWh'){ //取消维护批量
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '700px'],
                            title: '批量取消维护',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "stopMaintain.html?serverIds=" + serverIds + "&name=" + namesEncoded
                        });
                    }

                    if(obj.event === 'addkq'){ //批量修改状态
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '700px'],
                            title: '批量开启',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "editStatus.html?serverIds=" + serverIds + "&name=" + namesEncoded
                        });
                    }



                })

                table.on('tool(currentTableFilter)', function (obj) {
                    var data = obj.data;
                    if (obj.event==='edit'){
                        layer.open({
                            type: 2, // page 层类型
                            area: ['780px', '700px'],
                            title: '编辑区服',
                            shade: 0.6, // 遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "detail.html?id="+data.server_id
                        });
                    } else if(obj.event==='delete'){
                        layer.confirm('您确定删除吗?',{title:"提示"},function(){
                            $.post('/admin/server/'+data.id,{_method:'delete'},function(res){
                                if (res.code === 0) {
                                    layer.msg('success', {icon: 6, time: 500}, function () {
                                        location.reload();//父页面刷新
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 5})
                                }
                            })
                        })
                    } else if(obj.event === 'maintain') {
                        layer.open({
                            type: 2, //page 层类型
                            area: ['780px', '700px'],
                            title: '区服维护',
                            shade: 0.6, //遮罩透明度
                            shadeClose: true,
                            maxmin: true, // 允许全屏最小化
                            content: "maintain.html?id="+data.server_id
                        });
                    } else if(obj.event === 'stopMaintain'){
                        layer.confirm('您确定取消吗?',{title:"提示"},function(){
                            $.post('/admin/server/'+data.server_id+'/stopMaintain',{_method:"PUT"},function(res){
                                if (res.code === 0) {
                                    layer.msg('success', {icon: 6, time: 500}, function () {
                                        location.reload();//父页面刷新
                                    })
                                } else {
                                    layer.msg(res.msg, {icon: 5})
                                }
                            })
                        })
                    }
                });

            });
        }
    })

</script>
</body>
</html>